<template>
  <div class="addMerchantBox" v-loading="loading">
    <el-button
      class="el-icon-arrow-left"
      size="mini"
      type="primary"
      style="margin-bottom:10px;"
      @click="handBackList"
    >返回</el-button>
    <el-form ref="form" :model="initial_data" :rules="rules">

      <el-form-item label="节点链接" prop="url">
        <el-input v-model="initial_data.url" style="width:300px" placeholder="请输入节点链接"></el-input>
      </el-form-item>

      <el-form-item label="请求方式" prop="method">
        <el-input v-model="initial_data.method" style="width:300px" placeholder="请输入请求方式"></el-input>
      </el-form-item>

    </el-form>

    <div class="submitBtnBox">
      <el-button class="submitBtn" @click="submitFn" type="primary">确认修改</el-button>
    </div>
  </div>
</template>

<script>
export default {
  name: "permission-node-update",
  components: {},
  data() {
    return {
      initial_data: {
        //获取初始数据
        token: localStorage.getItem("user_token"), //token
        url: "",
        id: "",
        method: "",
      },
      loading: false,
      rules: {
        url: [{ required: true, message: "请输入节点链接", trigger: "blur" }],
        method: [{ required: true, message: "请输入请求方式", trigger: "blur" }]
      }
    };
  },
  watch: {},
  methods: {

defaultFn() {
      this.loading = true;
      this.$http
        .get("/web/user/url/info", {
          params: {
            token: localStorage.getItem("user_token"),
            id: this.$route.query.id
          }
        })
        .then(response => {
          if (response.data.status == "success") {
            this.initial_data = response.data.data;
            this.initial_data.token = localStorage.getItem("user_token");
            this.initial_data.id = this.$route.query.id
            this.loading = false;
          }
        })
        .catch(error => {
          console.log("error:", error);
        });
    },
    handBackList() {
      this.$router.push({ name: "permissionNode" });
    },

    submitFn() {
      this.$refs.form.validate(valid => {
        if (!valid) {
          return false;
        } else {
          this.loading = true;
          this.$nextTick(() => {
            this.$http.post("/web/user/url/update", this.initial_data).then(response => {
                if (response.data.status == "success") {
                  this.$message({
                    message: "修改成功",
                    duration: 6000
                  });

                  this.$router.push({
                    name: "permissionNode"
                  });
                } else {
                  this.$message({
                    message: "修改失败，原因：" + response.data.failedMsg,
                    duration: 6000
                  });
                }
                this.loading = false;
              })
              .catch(error => {
                this.$message({
                  message: "修改失败，原因：" + error,
                  duration: 6000
                });
                this.loading = false;
              });
          });
        }
      });
    }
  },
  mounted() {
      this.defaultFn();
  }
};
</script>

<style scoped lang="scss">
@import "./roles.scss";
</style>
